package com.netted.operationassistant.application;

import android.app.Application;
import android.content.Context;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.sdk.android.push.CloudPushService;
import com.alibaba.sdk.android.push.CommonCallback;
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.baidu.idl.face.platform.FaceSDKManager;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.Utils;
import com.netted.operationassistant.constant.Constant;
import com.netted.operationassistant.http.HttpRequest;
import com.netted.operationassistant.sharedPreferences.SharedPreferencesHelper;
import com.netted.operationassistant.utils.CrashHandler;

import java.io.File;
import java.io.IOException;
import java.security.InvalidParameterException;
import java.util.HashMap;
import java.util.Map;

import android_serialport_api.SerialPort;
import android_serialport_api.SerialPortFinder;

/**
 * Created by lu on 2018/6/22.
 */

public class BaseApplication extends Application {
    public static BaseApplication sInstance;
    //Volley的全局请求队列
    public static RequestQueue requestQueue;
    private static final String TAG = "Init";

    private static BaseApplication instance;

    //百度系
    public static boolean isLivenessRandom = false;
    /**
     * @return Volley全局请求队列
     */
    public static RequestQueue getRequestQueue() {
        return requestQueue;
    }


    @Override
    public void onCreate() {
        super.onCreate();
        sInstance = this;
        instance = this;
        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init(getApplicationContext());

        //实例化Volley全局请求队列
        requestQueue = Volley.newRequestQueue(getApplicationContext());
        Utils.init(this);

        initCloudChannel(this);

        FaceSDKManager.getInstance().initialize(this, Constant.licenseID, Constant.licenseFileName);

    }

    public static BaseApplication getInstance() {
        return instance;
    }

    /**
     * 初始化云推送通道
     * @param applicationContext
     */
    private void initCloudChannel(Context applicationContext) {
        PushServiceFactory.init(applicationContext);
        final CloudPushService pushService = PushServiceFactory.getCloudPushService();
        pushService.register(applicationContext, new CommonCallback() {
            @Override
                public void onSuccess(String response) {
                LogUtils.e("###################  init cloudchannel success "+response+"     CloudPushService.getDeviceId: "+pushService.getDeviceId());
                SharedPreferencesHelper sharedPreferencesHelper= new SharedPreferencesHelper(BaseApplication.this, Constant.XML_NAME);

                sharedPreferencesHelper.put(Constant.XML_INIT_ALIYUN,false);
            }
            @Override
            public void onFailed(String errorCode, String errorMessage) {
                LogUtils.e("init cloudchannel failed -- errorcode:" + errorCode + " -- errorMessage:" + errorMessage);
            }
        });


    }




    public SerialPortFinder mSerialPortFinder = new SerialPortFinder();
    private SerialPort mSerialPort = null;

    public SerialPort getSerialPort() throws SecurityException, IOException, InvalidParameterException {
        if (mSerialPort == null) {
//            String path = "/dev/ttyS0";
            String path = "/dev/ttyS3";
//            String path = "/dev/ttyUSB0";
            int baudrate = 19200;


			/* Open the serial port */
            mSerialPort = new SerialPort(new File(path), baudrate, 0);
        }
        return mSerialPort;
    }

    public void closeSerialPort() {
        if (mSerialPort != null) {
            mSerialPort.close();
            mSerialPort = null;
        }
    }



}
